package com.linkoog.devtools.code_generator.action;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import com.linkoog.devtools.code_generator.wizard.ModelStep;
import com.linkoog.devtools.code_generator.wizard.ModelWizardContext;
import com.linkoog.devtools.code_generator.wizard.model.GenModelFieldStep;
import com.linkoog.devtools.intellij.wizard.WizardDialog;
import com.linkoog.devtools.intellij.wizard.WizardStep;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

public class CodeGeneratorAction extends AnAction implements DumbAware {

    @Override
    public void actionPerformed(@NotNull AnActionEvent event) {
        Project project = event.getProject();
        if (project == null){
            return;
        }

        Module module = event.getData(LangDataKeys.MODULE);
        if (module == null){
            return;
        }

        VirtualFile selectedPackageVF = event.getData(LangDataKeys.VIRTUAL_FILE);
        if (selectedPackageVF == null || !selectedPackageVF.isDirectory()){
            return;
        }

        String selectedPackageName = ProjectRootManager.getInstance(project).getFileIndex().getPackageNameByDirectory(selectedPackageVF);
        if (StringUtils.isBlank(selectedPackageName)) {
            selectedPackageName = "<default>";
        }

        PsiDirectory basePsiDirectory = PsiManager.getInstance(project).findDirectory(selectedPackageVF);
        if (basePsiDirectory == null){
            return;
        }

        ModelWizardContext wizardContext = new ModelWizardContext();
        wizardContext.setProject(project);
        wizardContext.setModule(module);
        wizardContext.setSelectedPackageVF(selectedPackageVF);
        wizardContext.setBasePsiDirectory(basePsiDirectory);

        WizardDialog wizardDialog = new WizardDialog(project, wizardContext,"Create Model",new WizardStep[]{
                new ModelStep(project, wizardContext)
        });
        wizardDialog.show();

    }

    @Override
    public void update(@NotNull AnActionEvent event) {
        Project project = event.getProject();
        Module module = event.getData(LangDataKeys.MODULE);
        VirtualFile actionFolder = event.getData(LangDataKeys.VIRTUAL_FILE);

        if (project != null && module != null && actionFolder != null && actionFolder.isDirectory()) {
            String packageName = ProjectRootManager.getInstance(project).getFileIndex().getPackageNameByDirectory(actionFolder);
            event.getPresentation().setVisible(packageName != null);
        } else {
            event.getPresentation().setVisible(false);
        }
    }

    @Override
    public @NotNull ActionUpdateThread getActionUpdateThread() {
        return ActionUpdateThread.BGT;
    }
}
